package com.opennews.openplatform.familyexpenseapi.controller

import com.opennews.openplatform.familyexpenseapi.dto.account.group.AccountGroupDto
import com.opennews.openplatform.familyexpenseapi.service.AccountGroupService
import com.opennews.openplatform.familyexpenseapi.service.IncomeExpenseDetailService
import com.opennews.openplatform.myspringbootcore.dto.ApiResponse
import com.opennews.openplatform.myspringbootcore.util.getSuccessResponse
import com.opennews.openplatform.myspringbootmvccore.security.RequestIdentityExtractor
import jakarta.validation.Valid
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/account-group")
class AccountGroupController(
    private val accountGroupService: AccountGroupService,
    private val incomeExpenseDetailService: IncomeExpenseDetailService,
    private val requestIdentityExtractor: RequestIdentityExtractor,
) {
    @GetMapping("/query-account-group-info")
    fun queryAccountGroupInfo(): ResponseEntity<ApiResponse<Map<String, Any?>>> {
        val claims = requestIdentityExtractor.getClaims()
        val accountGroupId = claims.accountGroupId.asString()
        val userId = claims.id.asString()
        val result = accountGroupService.queryAccountGroupInfo(accountGroupId, userId)

        return getSuccessResponse(result)
    }

    @GetMapping("/query-basic-statistics")
    fun queryBasicStatistics(): ResponseEntity<ApiResponse<Map<String, Any?>>> {
        val claims = requestIdentityExtractor.getClaims()
        val accountGroupId = claims.accountGroupId.asString()
        val result = incomeExpenseDetailService.queryBasicStatistics(accountGroupId)

        return getSuccessResponse(result)
    }

    @PostMapping("/update")
    fun update(@Valid @RequestBody data: AccountGroupDto): ResponseEntity<ApiResponse<Any?>> {
        accountGroupService.update(data)
        return getSuccessResponse()
    }
}